//
//  WineNewFriendsViewController.m
//  Shengzhuli
//
//  Created by subook on 16/9/5.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "WineNewFriendsViewController.h"
#import "IconViewCell.h"
#import "InfoCell.h"
#import "SZLAccountHelper.h"
#import "FriendsDetailModel.h"
#import "WineFriendsNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "UIAlertView+Entension.h"

@interface WineNewFriendsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) FriendsDetailModel *model;
@property (nonatomic,weak) UIButton *sureBtn;

@end

@implementation WineNewFriendsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.userModel.nickname;
    [self requestWineFriendsInfo];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 120;
    }else{
        if (self.model.futures.count>0) {
            return 213+WineCollectionHeight;
        }
        return 170;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        IconViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IconViewCell class])];
        cell.imageStr = self.model.portraitUrl;
        return cell;
    }else {
        InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InfoCell class])];
        cell.model = self.model;
        cell.clipsToBounds = YES;
        return cell;
    }
}
- (void)sureClick
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认添加好友吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alert show];
    [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
        if (btnIndex == 1) {
            [self applyAddFriend];
        }
    }];
}

#pragma mark ---------------请求好友信息---------------------------
- (void)requestWineFriendsInfo
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"mobilePhone"] = self.userModel.buddy;
    __WEAK
    [self showWaiter];
    [WineFriendsNetwork getWineFriendsInfoWithParame:params response:^(BOOL success, NSString *msg,FriendsDetailModel *model) {
        [weakSelf hideWaiter];
        if (success) {
            self.model = model;
            [self.view addSubview:self.tableView];
            if (model.isFriend) {
                self.sureBtn.hidden = YES;
            }
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
//添加好友
- (void)applyAddFriend
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"friendMobilePhone"] = self.userModel.buddy;
    __WEAK
    [self showWaiter];
    [WineFriendsNetwork applyAddFriendsWithParame:params response:^(BOOL success, NSString *msg) {
        [weakSelf hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"已发送添加好友申请" toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH-64))];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGBColorOne(240);
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([IconViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([IconViewCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([InfoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([InfoCell class])];
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 60)];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(20, 10, ScreenW-40, 40);
        [btn setTitle:@"添加好友" forState:UIControlStateNormal];
        btn.titleLabel.textColor = [UIColor whiteColor];
        btn.titleLabel.font = Font(18);
        [btn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateNormal];
        [btn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:btn];
        self.sureBtn = btn;
    }
    return _footerView;
}
@end
